﻿Imports Brainworklite.Entity

Public Class Loader(Of T)

    Private _CurrentObject As Entity.EntityBase
    Public Property CurrentObject() As Entity.EntityBase
        Get
            Return _CurrentObject
        End Get
        Set(ByVal value As Entity.EntityBase)
            _CurrentObject = value
        End Set
    End Property
    Public Sub New(ByVal obj As Object)
        If Not TypeOf obj Is Brainworklite.Entity.EntityBase Then
            Throw New ApplicationException("El control no implementa entitybase")
        End If

        Me.CurrentObject = DirectCast(obj, Entity.EntityBase)
    End Sub


    Public Function getControlCollection() As List(Of Control)
        Dim lp = CurrentObject.PropertiesAttributes()
        Dim _controlCollection = New List(Of Control)()

        For Each item In lp
            Dim o As New LabeledTexbox(item.Value)
            _controlCollection.Add(o)
        Next

        Return _controlCollection

    End Function



    Private _getBussinesLogic As BussinesLogicGeneric(Of T)
    Private ReadOnly Property getBL() As BussinesLogicGeneric(Of T)
        Get
            If _getBussinesLogic Is Nothing Then
                _getBussinesLogic = New BussinesLogicGeneric(Of T)()
            End If
            Return _getBussinesLogic
        End Get

    End Property

    Private Function CreateInstance() As T
        Return Activator.CreateInstance(Of T)()
    End Function

    Private Function CreateInstanceOfEntityBase() As Entity.EntityBase
        Return CType(CObj(CreateInstance()), EntityBase)
    End Function

    Private _CurrentInstace As EntityBase
    Private ReadOnly Property EntityBaseInstance() As Entity.EntityBase
        Get
            If _CurrentInstace Is Nothing Then
                _CurrentInstace = CreateInstanceOfEntityBase()
            End If
            Return _CurrentInstace
        End Get
    End Property


    Private ReadOnly Property ClassAttributes As Attributes.SelfMappClassAttribute
        Get
            Return EntityBaseInstance.ClassAttribute
        End Get
    End Property

    Private ReadOnly Property PropertiesAtt As Dictionary(Of String, Attributes.PropertiesAttributes)
        Get
            Return EntityBaseInstance.PropertiesAttributes
        End Get
    End Property



    Public Sub FullFill(ByVal ctl As ComboBox)


        FullFill(ctl, EntityBase.DataValueFieldName, EntityBase.DataTextFieldName)

    End Sub

    Public Sub FullFillWithEmpty(ByVal ctl As ComboBox, _
                                 Optional ByVal Emptymessage As String = Nothing)

        Dim DataSource As New List(Of Object)
        If Not String.IsNullOrEmpty(Emptymessage) Then
            DataSource.Add(New With {.DataValueField = Nothing, .DataTextField = Emptymessage})
        Else

            DataSource.Add(New With {.DataValueField = Nothing, .DataTextField = "------Select-----"})
        End If


        DataSource.AddRange(getBL().getList())
        ctl.DataSource = DataSource.Select(Function(x) New With {.DataValueField = x.DataValueField, .DataTextField = x.DataTextField}).ToList()
        ctl.ValueMember = EntityBase.DataValueFieldName
        ctl.DisplayMember = EntityBase.DataTextFieldName

    End Sub

    Public Sub FullFillWithEmpty(ByVal ctl As ComboBox, _
                                 ByVal qe As WhereQueryExpression, _
                                 Optional ByVal Emptymessage As String = Nothing)
        Dim DataSource As New List(Of Object)

        If Not String.IsNullOrEmpty(Emptymessage) Then
            DataSource.Add(New With {.DataValueField = Nothing, .DataTextField = Emptymessage})
        Else

            DataSource.Add(New With {.DataValueField = Nothing, .DataTextField = "------Select-----"})
        End If


        DataSource.AddRange(getBL().getDataTextValueList(qe))
        ctl.DataSource = DataSource.Select(Function(x) New With {.DataValueField = x.DataValueField, .DataTextField = x.DataTextField}).ToList()
        ctl.ValueMember = EntityBase.DataValueFieldName
        ctl.DisplayMember = EntityBase.DataTextFieldName

    End Sub

    Public Sub FullFillDistinctWithEmpty(ByVal ctl As ComboBox, _
                              ByVal qe As WhereQueryExpression, _
                              Optional ByVal Emptymessage As String = Nothing)
        Dim DataSource As New List(Of Object)

        If Not String.IsNullOrEmpty(Emptymessage) Then
            DataSource.Add(New With {.ValueMember = Nothing, .DisplayMember = Emptymessage})
        Else

            DataSource.Add(New With {.ValueMember = Nothing, .DisplayMember = "------Select-----"})
        End If

        Dim ln = (From c In getBL().getDataTextValueList(qe) Select New With {Key c.ValueMember, c.DisplayMember} Distinct).OrderBy(Function(x) x.DisplayMember).ToList().Cast(Of Object)()



        DataSource.AddRange(ln)
        ctl.DataSource = DataSource.Select(Function(x) New With {.DataValueField = x.DataValueField, .DataTextField = x.DataTextField}).ToList()
        ctl.ValueMember = EntityBase.DataValueFieldName
        ctl.DisplayMember = EntityBase.DataTextFieldName

    End Sub

    Public Sub FullFill(ByVal ctl As ComboBox, ByVal qe As WhereQueryExpression)
        Dim dDataSource = getBL().getList(qe)
        ctl.DataSource = dDataSource

        ctl.ValueMember = EntityBase.DataValueFieldName
        ctl.DisplayMember = EntityBase.DataTextFieldName

    End Sub

    Public Sub FullFill(ByVal ctl As ComboBox, ByVal DataValueFieldName As String, ByVal DataTextFieldName As String)

        FullFill(ctl, getBL().getEntityBaseList(), DataValueFieldName, DataTextFieldName)
    End Sub

    Public Sub FullFill(ByVal ctl As ComboBox, ByVal DataSource As IEnumerable(Of T), ByVal DataValueFieldName As String, ByVal DataTextFieldName As String)
        ctl.DataSource = DataSource
        ctl.ValueMember = DataValueFieldName
        ctl.DisplayMember = DataTextFieldName

    End Sub

    Public Sub FullFill(ByVal ctl As ComboBox, ByVal DataSource As IEnumerable(Of EntityBase), ByVal DataValueFieldName As String, ByVal DataTextFieldName As String)
        ctl.DataSource = DataSource
        ctl.ValueMember = DataValueFieldName
        ctl.DisplayMember = DataTextFieldName

    End Sub

    Public Sub FullFill(ByVal ctl As ComboBox, ByVal DataSource As IEnumerable(Of T))
        ctl.DataSource = DataSource
        ctl.ValueMember = EntityBase.DataValueFieldName
        ctl.DisplayMember = EntityBase.DataTextFieldName


    End Sub



End Class
